<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <!--edge浏览器H5兼容设置-->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!--360浏览器H5兼容设置-->
    <meta name="renderer" content="webkit"/>
    <title>香蕉手机商城</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!--导入核心文件-->
    <script th:src="@{/bootstrap3/js/holder.js}"></script>
    <link th:href="@{/bootstrap3/css/bootstrap.css}" rel="stylesheet" type="text/css">
    <script th:src="@{/bootstrap3/jquery-1.9.1.min.js}"></script>
    <script th:src="@{/bootstrap3/js/bootstrap.js}"></script>
    <!-- 字体图标 -->
    <link rel="stylesheet" th:href="@{/bootstrap3/font-awesome-4.7.0/css/font-awesome.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/css/top.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/css/index.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/css/layout.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/css/login.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/css/footer.css}"/>
    <script th:src="@{/bootstrap3/js/jquery.cookie.js}" type="text/javascript" charset="utf-8"></script>
    <script th:src="@{/js/autoLogin.js}" type="text/javascript"></script>
</head>
<body>
<!--头部-->
<header class="header">
    <img th:src="@{/images/index/BananaPhone.png}" alt=""/>
    <span class="pull-right"><small>欢迎访问</small><b>香蕉手机商城</b></span>
</header>
<!--主体-->
<div class="container mycontent text-left">
    <!--透明层 -->
    <div class="loginDiv">
        <p>用户登录</p>
        <!--表单开始-->
        <form id="form-login" th:action="index.html" class="form-horizontal" role="form">
            <!--用户名-->
            <div class="form-group">
                <label for="username" class="col-md-3 control-label">名字：</label>
                <div class="col-md-8">
                    <input name="username" type="text" class="form-control" id="username" placeholder="请输入用户名">
                </div>
            </div>
            <!--密码-->
            <div class="form-group">
                <label for="password" class="col-md-3 control-label"> 密码：</label>
                <div class="col-md-8">
                    <input name="password" type="password" class="form-control" id="password" placeholder="请输入密码">
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-3 control-label"></label>
                <div class="col-md-8">
                    <a href="#" id="togglePassword">显示密码</a>
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-3 control-label"></label>
                <div class="col-md-8">
                    <input id="btn-login" class="btn btn-primary" type="button" value="登录"/>
                    <span class="pull-right"><small>还没有账号？</small><a th:href="@{/goRegister}">注册</a></span>
                </div>
            </div>
        </form>
    </div>
</div>

<!--页脚结束-->
<script type="text/javascript">
    $("#btn-login").click(function () {
        $.ajax({
            url: "/user/login",
            type: "POST",
            data: $("#form-login").serialize(),
            dataType: "JSON",
            success: function (rs) {
                if (rs.code == 200) {
                    alert("登录成功");
                    //跳转到主页
                    location.href = "/goIndex";
                    // 存储密码到 localStorage
                    localStorage.setItem("userPassword", $("input[name='password']").val());
                } else {
                    alert(rs.msg);
                }
            }
        });
    });


    document.getElementById('togglePassword').addEventListener('click', function (e) {
        e.preventDefault(); // 阻止默认的链接跳转行为
        const passwordField = document.getElementById('password');
        if (passwordField.type === 'password') {
            passwordField.type = 'text';
            this.textContent = '隐藏密码';
        } else {
            passwordField.type = 'password';
            this.textContent = '显示密码';
        }
    });
</script>
</body>
</html>